  dojo.require("esri.map");
      dojo.require("esri.toolbars.draw");
      dojo.require("esri.layers.FeatureLayer");
      dojo.require("esri.tasks.query");
      dojo.require("dojo.data.ItemFileReadStore");
      
      var map,toolbar;
      var featureLayer;
      var graphic;
      var currLocation;

      function init() {
        var initExtent = new esri.geometry.Extent({"xmin":37549.7524752475,"ymin":451800,"xmax":226450.247524752,"ymax":588200,"spatialReference":{"wkid":28992}});
        map = new esri.Map("map", {
          extent: initExtent
        });
        dojo.connect(map, 'onLoad', function(map) {
          //initialize the toolbar
          toolBar = new esri.toolbars.Draw(map);
          dojo.connect(toolBar, "onDrawEnd",onDrawEnd);          
          dojo.connect(dijit.byId('map'), 'resize', resizeMap);
	  dojo.connect(map, "onExtentChange", showExtent);
	  if(navigator.geolocation){  
            navigator.geolocation.getCurrentPosition(zoomToLocation, locationError);
            navigator.geolocation.watchPosition(showLocation, locationError);
          }
        });
        //var basemapUrl = "http://gis.noord-holland.nl/ArcGIS/rest/services/PNH_ondergrond_topo_kleur_tegels/MapServer";
	//var basemapUrl = "http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"
//var featureLayerUrl = "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Earthquakes/EarthquakesFromLastSevenDays/MapServer/0";
	var structuurvisieUrl = "http://gis.noord-holland.nl/ArcGIS/rest/services/PNH_structuurvisie/MapServer";
       	//var basemap = new esri.layers.ArcGISTiledMapServiceLayer(basemapUrl);
	var structuurvisielayer = new esri.layers.ArcGISDynamicMapServiceLayer(structuurvisieUrl,{opacity:.55});
        //featureLayer = new esri.layers.FeatureLayer(featureLayerUrl,{
        //  mode:esri.layers.FeatureLayer.MODE_ONDEMAND,
        //  outFields:["region,magnitude,depth,objectid"]
        //});
        //featureLayer.setSelectionSymbol(new esri.symbol.SimpleMarkerSymbol().setSize(8).setColor(new dojo.Color([160,214,238])));
      
	//map.addLayer(basemap);
	map.addLayer(structuurvisielayer);
        //map.addLayer(featureLayer);
      }
      function onDrawEnd(extent){
      toolBar.deactivate();
      //select features within the draw extent
      var query = new esri.tasks.Query();
      query.geometry = extent;
      featureLayer.selectFeatures(query,esri.layers.FeatureLayer.SELECTION_NEW,function(features,selectionMethod){
        //add selected features to the grid
        var items = dojo.map(features,function(feature){
          return feature.attributes;
        });
        var data = {identifier:"objectid", items:items};
        var store = new dojo.data.ItemFileReadStore({data:data});
        grid.setStore(store);
      });
      
      }

	 function showExtent(extent) {
        var s = "";
        s = "XMin: "+ extent.xmin.toFixed(2) + "&nbsp;"
           +"YMin: " + extent.ymin.toFixed(2) + "&nbsp;"
           +"XMax: " + extent.xmax.toFixed(2) + "&nbsp;"
           +"YMax: " + extent.ymax.toFixed(2);
        dojo.byId("footer").innerHTML = s;
      }

	function locationError(error) {
        switch (error.code) {
        case error.PERMISSION_DENIED:
          alert("Location not provided");
          break;

        case error.POSITION_UNAVAILABLE:
          alert("Current location not available");
          break;

        case error.TIMEOUT:
          alert("Timeout");
          break;

        default:
          alert("unknown error");
          break;
        }

      }
	 function zoomToLocation(location) {
        //var pt = esri.geometry.geographicToWebMercator(new esri.geometry.Point(location.coords.longitude, location.coords.latitude));    
	var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude);
        map.centerAndZoom(pt, 16);
      }

	 function showLocation(location) {
        //zoom to the users location and add a graphic
        //var pt = esri.geometry.geographicToWebMercator(new esri.geometry.Point(location.coords.longitude, location.coords.latitude));
	var pt = new esri.geometry.Point(location.coords.longitude, location.coords.latitude);
        if (!graphic) {
          var symbol = new esri.symbol.PictureMarkerSymbol('/js/images/bluedot.png', 40, 40);
          graphic = new esri.Graphic(pt, symbol);
          map.graphics.add(graphic);
        }
        else { //move the graphic if it already exists
          graphic.setGeometry(pt);
        }
        console.log("Accuracy: " + location.coords.accuracy);
        console.log("Speed: " + location.coords.speed);
        map.centerAt(pt);
      }

      function resizeMap() {
        //resize the map when the browser resizes - view the 'Resizing and repositioning the map' section in 
        //the following help topic for more details http://help.esri.com/EN/webapi/javascript/arcgis/help/jshelp_start.htm#jshelp/inside_guidelines.htm
        var resizeTimer;
        clearTimeout(resizeTimer);
        resizeTimer = setTimeout(function() {
          map.resize();
          map.reposition();
        }, 500);
      }
      //show map on load 
      dojo.addOnLoad(init);
